package com.tdkj.action.front.information;

import java.sql.Timestamp;


import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.tdkj.action.BaseAction;
import com.tdkj.model.financial.BeanFinancialAccount;
import com.tdkj.model.risevalue.BeanAdvert;
import com.tdkj.model.risevalue.BeanAdvertAuctionBidInfo;
import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;

public class AuctionAction  extends BaseAction{
	private Long id;
	private int currentprice;
	private int price;
	private BeanAdvert advert;
	private BeanAdvertAuctionInfo auctioninfo;
	private IBeanAdvertAuctionService auctionService;
	private IBeanAdvertAuctionService advertAuctionService;
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getCurrentprice() {
		return currentprice;
	}
	public void setCurrentprice(int currentprice) {
		this.currentprice = currentprice;
	}
	public IBeanAdvertAuctionService getAdvertAuctionService() {
		return advertAuctionService;
	}
	@Resource
	public void setAdvertAuctionService(
			IBeanAdvertAuctionService advertAuctionService) {
		this.advertAuctionService = advertAuctionService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public BeanAdvert getAdvert() {
		return advert;
	}
	public void setAdvert(BeanAdvert advert) {
		this.advert = advert;
	}
	public BeanAdvertAuctionInfo getAuctioninfo() {
		return auctioninfo;
	}
	public void setAuctioninfo(BeanAdvertAuctionInfo auctioninfo) {
		this.auctioninfo = auctioninfo;
	}
	public IBeanAdvertAuctionService getAuctionService() {
		return auctionService;
	}
	@Resource
	public void setAuctionService(IBeanAdvertAuctionService auctionService) {
		this.auctionService = auctionService;
	}
	public String findAll(){
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.addOrder(Order.desc("aendtime"));// 对状态进行排序
		this.queryByPage(criteria, "auctionAction!findAll.action", 25);
		return "findAll";
	}
	//获取竞价中的信息
	public String findAuctionIn(){
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.addOrder(Order.desc("status"));// 对状态进行排序
		this.queryByPage(criteria, "auctionAction!findAuctionIn.action", 10);
		return "auction_in_list";
	}
	//获取竞价历史
	public String findAuctionOut(){
		Criteria criteria = this.createCriteria(BeanAdvertAuctionInfo.class);
		criteria.addOrder(Order.desc("status"));// 对状态进行排序
		this.queryByPage(criteria, "auctionAction!findAuctionIn.action", 10);
		return "auction_out_list";
	}
	public String findAuctionBid(){
		auctioninfo=advertAuctionService.findById(this.getId(), BeanAdvertAuctionInfo.class);
		Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
		criteria.setProjection(Projections.projectionList().add(Projections.max("price")))
		.add(Restrictions.eq("auctioninfo", auctioninfo));
		Object o=(Integer) criteria.list().get(0);
		if(o==null)currentprice=0;
		else currentprice=(Integer) criteria.list().get(0);
		return "auction_bid_add";
	}
	public String addAuctionBid(){
		BeanAdvertAuctionInfo info=advertAuctionService.findById(this.getId(), BeanAdvertAuctionInfo.class);
		id=this.getId();
		BeanFinancialAccount c=this.getCurrentUser().getFinancialAccount();
		int count=c.getGold();
		int currentprice=this.getCurrentprice();
		if(count<=currentprice) {
			this.addFieldError("priceVlidate", "账户资金不足，请充值!");
		}
		BeanAdvertAuctionBidInfo b=new BeanAdvertAuctionBidInfo();
		b.setAuctioninfo(info);
		b.setAuctiontime(new Timestamp(System.currentTimeMillis()));
		b.setPrice(this.getPrice());
		b.setUser(this.getCurrentUser());
		b.setStatus("未成功");
		//扣款=================
		this.getBaseService().save(b);
		return findAuctionBid();
	}
}
